package com.example.demo.zaqizabatest;

import org.jaudiotagger.audio.AudioFileIO;
import org.jaudiotagger.audio.mp3.MP3AudioHeader;
import org.jaudiotagger.audio.mp3.MP3File;
import org.jaudiotagger.tag.id3.ID3v1Tag;

import java.io.File;

/**
 * @ClassName MusicInfo
 * @Author lys4134
 * @Date 2021/08/19 11:28:54
 * @Description MusicInfo
 * @Version 1.0
 **/
public class MusicInfo {
    private static Music getSongInfo(String filePath) {

        Music music = null;

        try {

            MP3File mp3File = (MP3File) AudioFileIO.read(new File(filePath));
            MP3AudioHeader audioHeader = (MP3AudioHeader) mp3File.getAudioHeader();
            ID3v1Tag tag = mp3File.getID3v1Tag();//获取ID3v1的信息

            String songname = String.valueOf(tag.getTitle());//歌名
            String artist = String.valueOf(tag.getArtist());//歌手
            String album = String.valueOf(tag.getAlbum());//专辑
            int duration = audioHeader.getTrackLength();//时长

            //封装到music对象
            music = new Music(songname, artist, album, duration, filePath);


        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("文件读取失败！"+e);
        }

        return music;
    }

    public static void main(String[] args) {
        Music songInfo = getSongInfo("src/main/resources/static/music/Kalimba.mp3");
        System.out.println(songInfo);
    }
}
